package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.manager.SeedPurchaseRecordManager;
import com.jiantong.work.model.SeedPurchaseRecord;
import com.jiantong.work.service.SeedPurchaseRecordWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by maowenjie on 2017/4/18.
 *
 */
@Slf4j
@Service
@RpcProvider
public class SeedPurchaseRecordWriteServiceImpl implements SeedPurchaseRecordWriteService {

    private final SeedPurchaseRecordManager seedPurchaseRecordManager;

    @Autowired
    public SeedPurchaseRecordWriteServiceImpl(SeedPurchaseRecordManager seedPurchaseRecordManager) {
        this.seedPurchaseRecordManager = seedPurchaseRecordManager;
    }


    @Override
    public Response<Map<String, Object>> createSeedPurchaseRecord(SeedPurchaseRecord seedPurchaseRecord, Integer toStatus) {
        try{
            return Response.ok(seedPurchaseRecordManager.createSeedPurchaseRecord(seedPurchaseRecord,toStatus));
        }catch (Exception e){
            log.error("failed to create seedPurchaseRecord error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("seedPurchaseRecord.create.failed");
        }
    }

    @Override
    public Response<Map<String, Object>> updateSeedPurchaseRecord(SeedPurchaseRecord seedPurchaseRecord, Integer toStatus) {
        try{
            return Response.ok(seedPurchaseRecordManager.updateSeedPurchaseRecord(seedPurchaseRecord,toStatus));
        }catch (Exception e){
            log.error("failed to update seedPurchaseRecord error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("seedPurchaseRecord.update.failed");
        }
    }

    @Override
    public Boolean update(SeedPurchaseRecord seedPurchaseRecord) {
        return seedPurchaseRecordManager.update(seedPurchaseRecord);
    }

}
